//
//  LoadViewController.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2019/12/23.
//  Copyright © 2019 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation


class LoadWindow: NSWindow {
    
    override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
        super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
        
        self.collectionBehavior = NSWindow.CollectionBehavior.fullScreenNone
        
        self.contentView?.wantsLayer = true
        self.backgroundColor = NSColor.clear
        
        self.backingType = .buffered
        self.isMovableByWindowBackground = true
        self.isMovable = true
        self.hasShadow = true
        //self.titlebarAppearsTransparent = true
        //self.titleVisibility = .hidden
        //self.toolbar?.showsBaselineSeparator = false
        //self.animationBehavior = .alertPanel
        self.collectionBehavior = NSWindow.CollectionBehavior.fullScreenPrimary
        self.styleMask = [.titled,.closable,.fullSizeContentView]
        self.title = "进入会议"
        self.standardWindowButton(NSWindow.ButtonType.zoomButton)?.isHidden = true
        self.standardWindowButton(NSWindow.ButtonType.miniaturizeButton)?.isHidden = true
        //self.standardWindowButton(NSWindow.ButtonType.closeButton)?.isHidden = true
    }
    
    public class func show() {
        let window = LoadWindow.init(contentViewController: LoadViewController.shared)
        window.orderFront(nil)
    }
    
    public class func dismss() {
        LoadViewController.shared.indicator.stopAnimation(nil)
        LoadViewController.shared.view.window?.orderOut(nil)
        LoadViewController.shared.view.window?.close()
    }
}

class LoadViewController: BaseViewController {
    static let shared = LoadViewController()
    var count:Double?
    var showTimer: Timer?
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, 400, 200))
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(230).cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        self.refactoringWindow()
    }
    
    func refactoringWindow() {
        self.view.window?.title = "进入会议"
    }
    
    func setupUI() {
        
        self.view.addSubview(tabarLine)
        tabarLine.snp.makeConstraints { (make) in
            make.height.equalTo(1)
            make.width.centerX.equalToSuperview()
            make.bottom.equalTo(-26)
        }
        
        self.view.addSubview(indicator)
        indicator.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-15)
        }
        
        self.view.addSubview(indicatorLab)
        indicatorLab.snp.makeConstraints { (make) in
            make.height.equalTo(18)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(20)
        }
    }
    
    func loadData() {
        self.indicator.startAnimation(nil)
    }
    
    lazy var indicator: NSProgressIndicator = {
        let view = NSProgressIndicator.init(frame: NSRect.zero)
        view.style = .spinning
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.clear.cgColor
        view.controlSize = .regular
        view.sizeToFit()
        return view
    }()
    
    lazy var indicatorLab: NSLabel = {
        let lab = NSLabel.label(title: "加载中", textColor: color_gray(80), font: NSFont.systemFont(ofSize: 12))
        lab.alignment = .center
        lab.sizeToFit()
        return lab
    }()
    
    
    lazy var tabarLine: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(200).cgColor
        return view
    }()
}
